<?php
class module_download extends module {
		
	function show() {
		$id = null;
		if (request::get("id")) {
			// request by ?id=VIDID
			$id = request::get("id");
		} else {
			// request by ?VIDID
			$request = request::get("*");
			unset($request["module"]);
			$keys = array_keys($request);
			if ($keys) {
				$id = $keys[0];
			}
		}
		if ($id) {
			$download = new download();
			$download->loadByField("video_id", $id);
			if (file_exists($absfile = config::getInstance()->site_root . "/download/".$id.".mp3")) {
				$download->hit();
				$video = new video($id);
				$title = $video->getMetaArtist() . " - " . $video->getMetaTitle();
				
//				$id3 = new ID3v1x($absfile);
//				$id3->read_tag();
//				
//				if ($id3->write_tag(1,
//					$video->getMetaTitle(),
//					$video->getMetaArtist(),
//					$id3->album,
//					$id3->year,
//					"Free downloads by http://www.vdjo.com",
//					$id3->track,
//					$id3->genre
//				) !== true) {
//					die("error");
//				}
				
				// send headers
				ob_end_clean();
				header('Content-Type: audio/mpeg');
				header('Content-Disposition: attachment; filename="'.$title.'"'); 
				header('Content-Transfer-Encoding: binary');
				// load the file to send
				readfile($absfile);
				exit();
			}
		}
		error::http404();
	}	
	
}

?>